﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using UnityEngine;

//-------------------------------------------------------------------------
public struct LocalVersionConfig
{
    public string bundle_version;
    public string data_version;
    public string remote_version_url;
}

//-------------------------------------------------------------------------
public struct RemoteVersionConfig
{
    public string bundle_version;
    public string bundle_url;
    public string data_version;
    public string data_url;
    public _eServerState server_state;
}

//-------------------------------------------------------------------------
public enum _eServerState
{
    Normal,
    Maintenance,
}

public class VersionConfig
{
    //-------------------------------------------------------------------------
    public LocalVersionConfig LocalVersionConfig { get; private set; }
    public RemoteVersionConfig RemoteVersionConfig { get; private set; }
    public Dictionary<string, string> MapLocalAssetInfo { get; private set; }
    public Dictionary<string, string> MapRemoteAssetInfo { get; private set; }
    public const string mDataVersionKey = "LocalDataVersionKey";
    public string LocalAssetInfoPath { get; private set; }

    //-------------------------------------------------------------------------
    public VersionConfig(string local_assetinfo_path)
    {
        LocalAssetInfoPath = local_assetinfo_path;

        MapLocalAssetInfo = new Dictionary<string, string>();
        MapRemoteAssetInfo = new Dictionary<string, string>();
        _loadClientVersionConfig();
        _loadClientPackInfo();
    }

    //-------------------------------------------------------------------------
    void _loadClientVersionConfig()
    {
        TextAsset config = Resources.Load<TextAsset>("Config/VersionInfoConfig");
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(config.text);

        LocalVersionConfig client_version;
        XmlNode node_info =
#if UNITY_ANDROID
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoAndroid");
#elif UNITY_IPHONE
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoIOS");
#else
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoPC");
#endif
        client_version.bundle_version = node_info.Attributes["BundleVersion"].Value;
        string data_version = "";
        if (PlayerPrefs.HasKey(mDataVersionKey))
        {
            data_version = PlayerPrefs.GetString(mDataVersionKey);
        }
        else
        {
            data_version = node_info.Attributes["DataVersion"].Value;
        }
        client_version.data_version = data_version;
        client_version.remote_version_url = node_info.Attributes["ServerVersionURL"].Value;
        LocalVersionConfig = client_version;
    }

    //-------------------------------------------------------------------------
    void _loadClientPackInfo()
    {
        if (File.Exists(LocalAssetInfoPath))
        {
            _checkPackInfo(File.ReadAllText(LocalAssetInfoPath), true);
        }
        else
        {
            Debug.Log("不存在:: " + LocalAssetInfoPath);
        }
    }

    //-------------------------------------------------------------------------
    public void changeLocalDataVersionToRemote()
    {
        PlayerPrefs.SetString(mDataVersionKey, RemoteVersionConfig.data_version);
        LocalVersionConfig client_version = LocalVersionConfig;
        client_version.data_version = RemoteVersionConfig.data_version;
        LocalVersionConfig = client_version;
    }

    //-------------------------------------------------------------------------
    public void loadRemoteVersionConfig(string config)
    {
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(config);

        XmlNode version_node =
#if UNITY_ANDROID
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoAndroid");
#elif UNITY_IPHONE
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoIOS");
#else
        xml.SelectSingleNode("VersionInfoConfig/VersionInfoPC");
#endif
        XmlNode server_state_node = xml.SelectSingleNode("VersionInfoConfig/ServerState");

        RemoteVersionConfig server_config;
        server_config.bundle_version = version_node.Attributes["BundleVersion"].Value;
        server_config.bundle_url = version_node.Attributes["BundleURL"].Value;
        server_config.data_version = version_node.Attributes["DataVersion"].Value;
        server_config.data_url = version_node.Attributes["DataURL"].Value;
        server_config.server_state = (_eServerState)(int.Parse(server_state_node.Attributes["state"].Value));
        RemoteVersionConfig = server_config;
    }

    //-------------------------------------------------------------------------
    public void LoadRemotePackInfo(string pack_info)
    {
        _checkPackInfo(pack_info, false);
    }

    //-------------------------------------------------------------------------
    void _checkPackInfo(string pack_info, bool is_local)
    {
        string[] infos = pack_info.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var i in infos)
        {
            if (string.IsNullOrEmpty(i))
            {
                continue;
            }

            string[] info = i.Split(' ');
            if (is_local)
            {
                if (info.Length != 2)
                {
                    //FloatMsgInfo msg_info;
                    //msg_info.msg = "本地资源文件损坏,请修复文件!";
                    //msg_info.color = Color.red;
                    //UiMgr.Instance.FloatMsgMgr.createFloatMsg(msg_info);
                }
                else
                {
                    MapLocalAssetInfo[info[0]] = info[1];
                }
            }
            else
            {
                if (info.Length != 2)
                {
                    //FloatMsgInfo msg_info;
                    //msg_info.msg = "资源文件损坏!";
                    //msg_info.color = Color.red;
                    //UiMgr.Instance.FloatMsgMgr.createFloatMsg(msg_info);
                }
                else
                {
                    MapRemoteAssetInfo[info[0]] = info[1];
                }
            }
        }
    }
}
